// var model = require('./models/model');

// var User = model.User,
// 	Pet = model.Pets,
// 	Color = model.Color;


// function createUser() {
// 	User.create({
// 		name: "Jack",
// 		age: 22
// 	}).then(function(data) {
// 		// console.log('User create success: ' + JSON.stringify(data));
// 		console.log('User create success.');

// 		createPet(data.id);
// 		createColor(data.id);
// 	}).catch(function(error) {
// 		console.log('User create failed: ' + error);
// 	});
// }

// function createPet(id) {
// 	Pet.create({
// 		ownerId: id,
// 		name: 'Garfield',
// 		gender: 'female',
// 		birth: '2017-08-14'
// 	}).then(function(data) {
// 		console.log('Pet create success: ' + JSON.stringify(data));
// 	}).catch(function(error) {
// 		console.log('Pet create failed: ' + error);
// 	});
// }

// function createColor(id) {
// 	console.log('createColor IN: id=' + id);
// 	Color.create({
// 		name: "yellow",
// 		ownerId: id
// 	}).then(function(data) {
// 		console.log('Color create success: ' + JSON.stringify(data));
// 	}).catch(function(error) {
// 		console.log('Color create failed: ' + error);
// 	});
// }

// function queryUser() {
// 	User.findAll({
// 		where: {
// 			age: 22
// 		}
// 	}).then(function(data) {
// 		console.log('query user success: ' + JSON.stringify(data));
// 	}).catch(function(error) {
// 		console.log('query user failed: ' + error);
// 	});
// }

// function updatePet() {
// 	Pet.update({
// 		updatedAt: Date.now(),
// 		version: 1,
// 		name: 'Tom',
// 	},
// 	{
// 		where: {
// 			birth: '2017-08-14'
// 		}
// 	}).then(function(data) {
// 		console.log('update pet success: ' + JSON.stringify(data));
// 	}).catch(function(error) {
// 		console.log('update pet failed: ' + error);
// 	});
// }

// function deleteColor() {
// 	Color.destroy(
// 	{
// 		where: {
// 			name: 'red'
// 		}
// 	}).then(function(data) {
// 		console.log('delete color success: ' + JSON.stringify(data));
// 	}).catch(function(error) {
// 		console.log('delete color failed: ' + error);
// 	});
// }

// // createUser();
// // queryUser();
// // updatePet();
// deleteColor();

let express = require('express');
let app = express();
let bodyParser = require('body-parser');
let model = require('./models/model');
let path = require('path');

let User = model.User;

// app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());	// content-type=json

app.use(express.static(path.join(__dirname, '../public')));

app.get('/', (req, res) => {
	res.sendFile(path.join(__dirname, 'index.html'));
});

function addUser(params) {
	console.log('addUser() params: ' + JSON.stringify(params));

	return User.create(params);
}

function updateUser(params) {
	console.log('updateUser() params: ' + JSON.stringify(params));

	params.updatedAt = Date.now();
	return User.update(params,
	{
		where: {
			eid: params.eid
		}
	});
}

function getAllUser() {
	console.log('getAllUser()');

	return User.findAll({
		order: [
    		// Will escape username and validate DESC against a list of valid direction parameters
    		['createdAt', 'DESC']
    	]
	});
}

function deleteUser(eid) {
	console.log('deleteUser() eid: ' + eid);

	return User.destroy({
		where: {
			eid: eid
		}
	});
}

app.post('/api/v1/user/add', (req, res) => {
	let userInfo = {
		eid: req.body.eid ? req.body.eid : '',
		name: req.body.name ? req.body.name : '',
		gender: req.body.gender ? req.body.gender : '',
		age: req.body.age ? req.body.age : '',
		email: req.body.email ? req.body.email : '',
		phone: req.body.phone ? req.body.phone : '',
		address: req.body.address ? req.body.address : '',
	};

	addUser(userInfo).then((data) => {
		console.log('add user success.');

		res.end(JSON.stringify(data));
	}).catch((error) => {
		console.log('add user failed: ' + error);

		res.end(JSON.stringify(error));
	});
});

app.post('/api/v1/user/update', (req, res) => {
	let userInfo = {
		eid: req.body.eid ? req.body.eid : '',
		name: req.body.name ? req.body.name : '',
		gender: req.body.gender ? req.body.gender : '',
		age: req.body.age ? req.body.age : '',
		email: req.body.email ? req.body.email : '',
		phone: req.body.phone ? req.body.phone : '',
		address: req.body.address ? req.body.address : '',
	};

	updateUser(userInfo).then((data) => {
		console.log('update user success.');

		res.end(JSON.stringify(data));
	}).catch((error) => {
		console.log('update user failed: ' + error);

		res.end(JSON.stringify(error));
	});
});

app.get('/api/v1/user/getAll', (req, res) => {
	getAllUser().then((data) => {
		console.log('get all user success: ' + JSON.stringify(data));

		res.end(JSON.stringify(data));
	}).catch((error) => {
		console.log('get all user failed: ' + error);

		res.end(JSON.stringify(error));
	});
});

app.get('/api/v1/user/delete', (req, res) => {

	let eid = req.query.eid ? req.query.eid : "";

	deleteUser(eid).then((data) => {
		console.log('delete user success: ' + JSON.stringify(data));

		res.end(JSON.stringify(data));
	}).catch((error) => {
		console.log('delete user failed: ' + error);

		res.end(JSON.stringify(error));
	});
});


let server = app.listen(8088, () => {
	let host = server.address().address;
	let port = server.address().port;

	console.log('server url is: http://%s:%s', host, port);
});